#include "SwarmSceneBuffers.h"

#include <cassert>
#include <d3d11.h>

#include "main/Globals.h"

#include "managers/ShaderResourcesManager.h"

#include "scenes/swarmScene/SwarmSceneSettings.h"

namespace swarmScene {
	ConstantBuffers::ConstantBuffers(const Settings& settings) {
		// Geometry shader: Per Frame Buffer
		{
			D3D11_BUFFER_DESC bufferDesc;
			ZeroMemory(&bufferDesc, sizeof(D3D11_BUFFER_DESC));
			bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
			bufferDesc.ByteWidth = sizeof(mGSPerFrameBuffer.mData);
			bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
			bufferDesc.MiscFlags = 0;
			bufferDesc.StructureByteStride = 0;
			bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
			mGSPerFrameBuffer.mBuffer = Globals::gResources->addBuffer(L"gs_per_frame_buffer", bufferDesc, nullptr);
			assert(mGSPerFrameBuffer.mBuffer);
		}

		// Compute Shader: Immutable buffer
		{
			D3D11_SUBRESOURCE_DATA initData;
			ZeroMemory(&initData, sizeof(D3D11_SUBRESOURCE_DATA));
			initData.pSysMem = &settings.mDispatchGroupsY;

			D3D11_BUFFER_DESC bufferDesc;
			ZeroMemory(&bufferDesc, sizeof(D3D11_BUFFER_DESC));
			bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
			bufferDesc.ByteWidth = sizeof(mCSImmutableBuffer.mData);
			bufferDesc.CPUAccessFlags = 0;
			bufferDesc.MiscFlags = 0;
			bufferDesc.StructureByteStride = 0;
			bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
			mCSImmutableBuffer.mBuffer = Globals::gResources->addBuffer(L"cs_immutable_buffer", bufferDesc, &initData);
			assert(mCSImmutableBuffer.mBuffer);
		}
	}
}